草庐IT

C++ operator+ 和 operator+= 重载

全部标签

c++ - const T*& 和 const T* 不会在函数重载时产生歧义

为什么下面的示例代码不会产生歧义有没有办法调用第二个版本?(如果这不是错误)#includeusingnamespacestd;voidfoo(constint*){cout编辑:这个#includeusingnamespacestd;voidfoo(constint){cout确实会产生歧义。顺便说一下,去掉const产生的歧义。编译器:带有标志--std=c++14的g++5.3.0 最佳答案 Whythefollowingsamplecodedonotproduceambiguity这不是错误。参数的类型是constint*&

c++ - libstdc++ 和 libc++ : operator>> on bitset 行为差异

考虑以下代码:#include#include#includeintmain(intargc,char*argv[]){std::stringstreamstream;std::bitsetbitset(1);std::cout>bitset;std::cout在g++下用libstdc++编译,结果为:>g++bitset_empty.cpp-obitset_empty>./bitset_emptybefore=1after=1在clang++下用libc++编译,结果为:>clang++-stdlib=libc++bitset_empty.cpp-obitset_empty>./b

c++ - std::result_of 应用于 const 重载方法

如果我给typedefstd::vectorv;然后下面可以用来捕获常量迭代器的类型(另一种方法是使用v::const_iterator,但这取决于const_iterator成员类型在类中明确定义。typedeftypenamestd::result_of::typeconst_iterator;确实,我们可以检查上面的内容是否如我们所愿。static_assert(std::is_same::value);但是,我发现下面的编译器失败。typedeftypenamestd::result_of::typeiterator;编译器提示该方法被重载(通过const修饰符)并且无法明确解

C++ 是否需要重载运算符的特殊右值变体?

在类似OperatorOverloading的问题+答案中,据说重载二元运算符(例如operator+)的最佳方法是:classX{X&operator+=(constX&rhs){//actualadditionofrhsto*thisreturn*this;}};inlineXoperator+(Xlhs,constX&rhs){lhs+=rhs;returnlhs;}因此,operator+本身通过值获取lhs,通过const引用获取rhs,并通过值返回更改后的lhs.我无法理解如果使用右值作为lhs调用这里会发生什么:这仍然是需要的单一定义吗(编译器是否会优化参数和返回值),或

c++ - constexpr 求值分支/constexpr 重载

设置:我有一个使用SIMD内部函数的函数,我想在一些constexpr函数中使用它。为此,我需要将其设为constexpr。但是,SIMD内在函数没有标记为constexpr,编译器的常量求值器无法处理它们。我尝试用执行相同操作的C++constexpr实现替换SIMD内在函数。该函数在运行时变慢了3.5倍,但我能够在编译时使用它(是吗?)。问题:如何在常量表达式中使用这个函数而不减慢我的程序在运行时的速度?一些想法:为编译器常量表达式求值器添加对所有SIMD内在函数的常量求值支持,适用于所有编译器:可能是正确的解决方案,但却是一项不可能完成的艰巨任务。更务实的解决方案是:根据函数是否

c++ - 为什么这个用于检测类型 T 是否具有 void operator(EDT const&) 的 C++ 特性会失败?

我正在尝试使用SFINAE来检测作为模板参数T传递的类型是否具有T::operator()(Pconst&),其中P也是模板参数。我在MemberDetectorIdiom的这个例子之后为我的解决方案建模不幸的是,我无法让它为operator()工作,即使我可以让它为普通方法工作。下面是一些演示我面临的问题的示例代码:#include#include#include#includeusingnamespacestd;structhas{voidoperator()(intconst&);};structhasNot1{voidoperator()(int);};structhasNot

c++ - 为什么 float 和 int 的 std::pow 会调用此重载?

以下内容:#includeintmain(){floatbase=2.0f;floatresult=std::pow(base,2);return0;}在打开时触发-Wconversion警告。Wandbox似乎调用了std::pow的double重载,我希望选择float重载(与int指数被转换为float)。知道他重载的人能解释一下为什么吗? 最佳答案 自C++11起,混合参数pow将任何整数参数强制转换为double。混合参数函数的返回类型始终是double除非一个参数是longdouble然后结果是longdouble。[c

c++ - 如果一个类只有一个通过 require 启用的成员函数,它仍然可以被模棱两可地重载吗?

例如,这段代码有效吗?templatestructA{voidf()requiresstd::is_same_v{}voidf(int)requires!std::is_same_v{}};intmain(){autofptr=&A::f;return0;}不会compile使用gcc,但它似乎应该对我有用。 最佳答案 Ifaclasshasonlyasinglememberfunctionenabledviarequiresisitstillconsideredoverloaded?是的。[C++ConceptsTS:13/1]:

c++ - 在c++中为什么我们可以重载 `operator ->`而不能重载 `operator .`

这个问题在这里已经有了答案:Whycan'tyouoverloadthe'.'operatorinC++?(4个答案)关闭5年前。在c++中,为什么我们可以重载operator->而不能重载operator.?同样,为什么我们可以重载operator->*而不能重载operator.*呢?如果你能帮助我,我将不胜感激!

c++ - 我可以检测在编译时使用了哪个标签分派(dispatch)重载吗?

假设我有一个仿函数,它使用标签分派(dispatch)从函数的多个实现中进行选择,如下所示://baseclassforalltags,indicatingthe"default"implementationstructtag_base{};//subclassesfortagsthatmightselectadifferentimplementationstructtag1:tag_base{};structtag2:tag1{};structtag3:tag2{};structfunc{voidoperator()(tag_base){}voidoperator()(tag3){}